package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.QueryAppDetailReq;
import com.sg.dto.base.framework.req.QueryInductionRecordListReq;
import com.sg.dto.base.framework.req.QuerySpaceDetailReq;
import com.sg.dto.biz.framework.req.QueryMobileCurrentUserEmploymentRecordListComReqDto;
import com.sg.dto.biz.framework.req.QuerySpaceIdLookupDetailComReqDto;
import com.sg.dto.biz.framework.res.OrgInductionRecordDto;
import com.sg.dto.biz.framework.res.QueryMobileCurrentUserEmploymentRecordListComRespDto;
import com.sg.dto.biz.framework.res.QuerySpaceIdLookupDetailComRespDto;
import com.sg.entity.InductionRecord;
import com.sg.entity.UnionApp;
import com.sg.service.base.framework.MInductionRecordService;
import com.sg.service.base.framework.MSpaceService;
import com.sg.service.base.framework.MUnionAppService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class MobileFrameworkServiceImpl
        implements com.wicket.okrframework.biz.service.MobileFrameworkService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MSpaceService mSpaceService;
    @Resource
    private MUnionAppService mUnionAppService;
    @Resource
    private MInductionRecordService mInductionRecordService;

    /**
     * D1查询空间编号查空间ID详情等(公共)[9035]
     * gen by moon at 4/1/2024, 1:39:42 AM
     */
    @Trace(operationName = "D1查询空间编号查空间ID详情等(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QuerySpaceIdLookupDetailComRespDto querySpaceIdLookupDetailCom(QuerySpaceIdLookupDetailComReqDto reqDto) {


        Space space_1 = null;
        UnionApp unionApp_1 = null;
//步骤0: 1-2-04查询空间详情 - querySpaceDetail
        Space space = null;
        QuerySpaceDetailReq querySpaceDetailReq = new QuerySpaceDetailReq();
        if (reqDto != null) {
            querySpaceDetailReq.setOrganizationNumber(reqDto.getOrganizationNumber());//SimpleFieldAssign//sourceId:1682989_1
        }

        /*1-2-04查询空间详情[161]   */
        Assert.isNull(querySpaceDetailReq.getOrganizationNumber(), "D1查询空间编号查空间ID详情等(公共)-1-2-04查询空间详情-空间编号不能为空", false);
        space = mSpaceService.querySpaceDetail(querySpaceDetailReq)/*vcase invoke 本地 method 方法调用;*/;
        Assert.isTrue(space == null || space.getSpaceId() == null, "找不到数据，系统异常", false);

        space_1 = space;

//步骤1: 1-2-06查询联盟应用详情 - queryAppDetail
        UnionApp unionApp = null;
        if (space != null) {
            QueryAppDetailReq queryAppDetailReq = new QueryAppDetailReq();
            if (space != null) {
                queryAppDetailReq.setUniqueSpaceId(space.getSpaceId());//SimpleFieldAssign//sourceId:1683027_1
            }

            /*1-2-06查询联盟应用详情[168]   */
            Assert.isNull(queryAppDetailReq.getUniqueSpaceId(), "D1查询空间编号查空间ID详情等(公共)-1-2-06查询联盟应用详情-管理的空间ID不能为空", false);
            unionApp = mUnionAppService.queryAppDetail(queryAppDetailReq)/*vcase invoke 本地 method 方法调用;*/;


            unionApp_1 = unionApp;
        }

        QuerySpaceIdLookupDetailComRespDto retData = new QuerySpaceIdLookupDetailComRespDto();
        if (space_1 != null) {
            retData.setSpaceId(space_1.getSpaceId());//SimpleFieldAssign//sourceId:1683064_1
            retData.setOrganizationNumber(space_1.getOrganizationNumber());//SimpleFieldAssign//sourceId:1683066_1
            retData.setSpaceName(space_1.getSpaceName());//SimpleFieldAssign//sourceId:1683067_1
            retData.setSpaceLogo(space_1.getSpaceLogo());//SimpleFieldAssign//sourceId:1683068_1
        }
        if (unionApp_1 != null) {
            retData.setAppId(unionApp_1.getAppId());//SimpleFieldAssign//sourceId:1683065_1
            retData.setWebsite(unionApp_1.getWebsite());//SimpleFieldAssign//sourceId:1683069_1
            retData.setAppLogo(unionApp_1.getAppLogo());//SimpleFieldAssign//sourceId:1683070_1
            retData.setAboutLogo(unionApp_1.getAboutLogo());//SimpleFieldAssign//sourceId:1683071_1
            retData.setAppBanner(unionApp_1.getAppBanner());//SimpleFieldAssign//sourceId:1683072_1
            retData.setAppBigBanner(unionApp_1.getAppBigBanner());//SimpleFieldAssign//sourceId:1683073_1
            retData.setServicePhone(unionApp_1.getServicePhone());//SimpleFieldAssign//sourceId:1683074_1
            retData.setServiceTime(unionApp_1.getServiceTime());//SimpleFieldAssign//sourceId:1683075_1
            retData.setCopyright(unionApp_1.getCopyright());//SimpleFieldAssign//sourceId:1683076_1
            retData.setAppVersion(unionApp_1.getAppVersion());//SimpleFieldAssign//sourceId:1683077_1
            retData.setSenderMemberId(unionApp_1.getSenderMemberId());//SimpleFieldAssign//sourceId:1683078_1
        }


        return retData;
    }

    /**
     * D1查询移动端当前用户就职记录列表(公共)[8972]
     * gen by moon at 4/1/2024, 2:24:55 AM
     */
    @Trace(operationName = "D1查询移动端当前用户就职记录列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryMobileCurrentUserEmploymentRecordListComRespDto queryMobileCurrentUserEmploymentRecordListCom(QueryMobileCurrentUserEmploymentRecordListComReqDto reqDto) {


        List<InductionRecord> listInductionRecord_1 = new ArrayList<>();
//步骤0: 1-3-13查询就职记录列表 - queryInductionRecordList
        List<InductionRecord> listInductionRecord = new ArrayList<>();
        QueryInductionRecordListReq queryInductionRecordListReq = new QueryInductionRecordListReq();
        queryInductionRecordListReq.setInductionUnitTypeCode("DEPT");//sourceId:1681493_1
        queryInductionRecordListReq.setUserId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecUserId());//CURRENT_USERID//sourceId:1681492_1
        queryInductionRecordListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1681546_1
        queryInductionRecordListReq.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:1681496_1

        /*1-3-13查询就职记录列表[166]   */
        Assert.isNull(queryInductionRecordListReq.getInductionUnitTypeCode(), "D1查询移动端当前用户就职记录列表(公共)-1-3-13查询就职记录列表-就职单位类型实例不能为空", false);
        Assert.isNull(queryInductionRecordListReq.getUserId(), "D1查询移动端当前用户就职记录列表(公共)-1-3-13查询就职记录列表-个人账号ID不能为空", false);
        Assert.isNull(queryInductionRecordListReq.getSubjectLifeCycle(), "D1查询移动端当前用户就职记录列表(公共)-1-3-13查询就职记录列表-主体生命周期不能为空", false);
        Assert.isNull(queryInductionRecordListReq.getSpaceId(), "D1查询移动端当前用户就职记录列表(公共)-1-3-13查询就职记录列表-创建于空间ID不能为空", false);
        listInductionRecord = mInductionRecordService.queryInductionRecordList(queryInductionRecordListReq)/*vcase invoke 本地 method 方法调用;*/;


        listInductionRecord_1 = listInductionRecord;

        QueryMobileCurrentUserEmploymentRecordListComRespDto retData = new QueryMobileCurrentUserEmploymentRecordListComRespDto();
        retData.setOrgInductionRecordList(listInductionRecord_1.stream().map(item -> BeanUtil.toBean(item, OrgInductionRecordDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1682111_1


        return retData;
    }
    //
}
